import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '用户登录 - 汉中旅游服务平台' }
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { title: '首页 - 汉中旅游服务平台' }
  },
  {
    path: '/destinations',
    name: 'Destinations',
    component: () => import('@/views/DestinationPage.vue'),
    meta: { title: '景点介绍 - 汉中旅游服务平台' }
  },
  {
    path: '/destination/:id',
    name: 'DestinationDetail',
    component: () => import('@/views/DestinationDetail.vue'),
    meta: { title: '景点详情 - 汉中旅游服务平台' }
  },
  {
    path: '/routes',
    name: 'Routes',
    component: () => import('@/views/RoutesPage.vue'),
    meta: { title: '旅游攻略 - 汉中旅游服务平台' }
  },
  {
    path: '/route/:id',
    name: 'RouteDetail',
    component: () => import('@/views/RouteDetail.vue'),
    meta: { title: '路线详情 - 汉中旅游服务平台' }
  },
  {
    path: '/customize',
    name: 'Customize',
    component: () => import('@/views/CustomizePage.vue'),
    meta: { title: '私人定制 - 汉中旅游服务平台' }
  },
  {
    path: '/stories',
    name: 'Stories',
    component: () => import('../views/StoriesPage.vue'), // 确保路径正确
    meta: { title: '旅行故事 - 汉中旅游服务平台' }
  },
  {
    path: '/story/:id',
    name: 'StoryDetail',
    component: () => import('../views/StoryDetail.vue'), // 确保路径正确
    meta: { title: '故事详情 - 汉中旅游服务平台' }
  },
  {
    path: '/consult',
    name: 'Consult',
    component: () => import('@/views/ConsultPage.vue'),
    meta: { title: '预约咨询 - 汉中旅游服务平台' }
  },
  {
    path: '/new-page',
    name: 'MapNavigation',
    component: () => import('@/views/MapNavigationPage.vue'),
    meta: { title: '地图导航 - 汉中旅游服务平台' }
  },
  {
    path: '/test',
    name: 'TestApi',
    component: () => import('@/views/TestApi.vue'),
    meta: { title: 'API测试 - 汉中旅游服务平台' }
  },
  {
    path: '/user/profile',
    name: 'UserProfile',
    component: () => import('@/views/UserProfile.vue'),
    meta: { title: '个人中心 - 汉中旅游服务平台' }
  },
  {
    path: '/story/publish',
    name: 'StoryPublish',
    component: () => import('@/views/StoryPublish.vue'),
    meta: { title: '发布故事 - 汉中旅游服务平台' }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '汉中旅游服务平台'
  next()
})

export default router